package com.blockchain.front.servlets.trading.ajax;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.service.ServiceSession;
import com.blockchain.front.service.financial.FinancialManage;
import com.blockchain.front.servlets.trading.AbstractTradingServlet;
import com.blockchain.front.util.JsonUtil;
import com.blockchain.util.parser.IntegerParser;
import com.cuohe.MakeMatch;
import com.cuohe.models.BuyEntity;
import com.cuohe.models.SellEntity;

public class XxWdWt extends AbstractTradingServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		PrintWriter out = response.getWriter();
		int wtid = IntegerParser.parse(request.getParameter("wtid"));
		int scid = IntegerParser.parse(request.getParameter("scid"));
		Map<String,Object> result  = new HashMap<>();
		result.put("num", "0");
		result.put("msg", "");
		FinancialManage manage = serviceSession.getService(FinancialManage.class);
		if (scid <= 0) {
			result.put("msg", "市场异常");
			out.write(JsonUtil.GsonString(result));
			return;
		}
		try {
			manage.cxWt(wtid, scid);
			//向撮合系统增加撮合记录
			MakeMatch match = (MakeMatch)request.getServletContext().getAttribute("CHUANGKE_MATCH");
			if(null!=match) {
				match.removeBuyOrSellId(wtid);
				if(match.getBuyMap().containsKey(wtid)) {
					BuyEntity buy = match.removeBuyId(wtid);
					System.out.println("已撤销了买委托："+buy.getId());
				}
				if(match.getSellMap().containsKey(wtid)) {
					SellEntity sell = match.removeSellId(wtid);
					System.out.println("已撤销了卖委托："+sell.getId());
				}
			}
			result.put("num", "1");
			result.put("msg", "");
			out.write(JsonUtil.GsonString(result));
		} catch (Throwable e) {
			result.put("msg", e.getMessage());
			out.write(JsonUtil.GsonString(result));
			System.out.println(JsonUtil.GsonString(result));
			return;
		}
	}
}
